package com.atf.atfconfigurator.views;

import org.apache.commons.configuration.PropertiesConfiguration;

import com.atf.atfconfigurator.AtfconfiguratorUI;
import com.atf.atfconfigurator.composite.CollapseableBox;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

public class VirtualHostView extends VerticalLayout implements View {
	private PropertiesConfiguration wasConfigProperty;
	@Override
	public void enter(ViewChangeEvent event) {
		// TODO Auto-generated method stub
		wasConfigProperty=((AtfconfiguratorUI) getUI()).getWasConfiguration();	
		setSpacing(true);
        setMargin(true);

        Label title = new Label(wasConfigProperty.getString("appName"));
        title.addStyleName("h2");
        title.addStyleName("colored");
        addComponent(title);
        
		final FormLayout form = new FormLayout();
        form.setMargin(false);
        form.setSpacing(true);
        form.setWidth("100%");
        //form.set
        form.addStyleName("light");
        addComponent(form);
        
        //createVirtualHost
        CheckBox createVirtualHost = new CheckBox("Create VirtualHost");
        if( wasConfigProperty.getString("createVirtualHost", "NO").equalsIgnoreCase("YES")){
        	createVirtualHost.setValue(true);
        }
        createVirtualHost.addValueChangeListener(new Property.ValueChangeListener(){

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void valueChange(ValueChangeEvent event) {
				// TODO Auto-generated method stub
				if( (Boolean) event.getProperty().getValue()){
					((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("createVirtualHost", "YES");
				}else{
					((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("createVirtualHost", "NO");
				}
			}        	
        });
        form.addComponent(createVirtualHost);
        
         final TextField noOfHostAlias = new TextField("Number of Host Alias");
	     int noOfHostAliasValue=wasConfigProperty.getInt("noOfHostAlias",0);
	     noOfHostAlias.setValue(String.valueOf(noOfHostAliasValue));
	     noOfHostAlias.setWidth("50%");
	     form.addComponent(noOfHostAlias);
	     noOfHostAlias.addValueChangeListener(new Property.ValueChangeListener(){

				/**
				 * 
				 */
				private static final long serialVersionUID = 1L;

				@Override
				public void valueChange(ValueChangeEvent event) {
					// TODO Auto-generated method stub
					((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("noOfHostAlias",  (String) event.getProperty().getValue());
					((AtfconfiguratorUI) getUI()).navigator.navigateTo("VirtualHost");
				}        	
	        });
	     if(noOfHostAliasValue > 0){
	    	 for(int i=1; i<noOfHostAliasValue+1; i++){
	    		 form.addComponent(new CollapseableBox(String.valueOf(i),getHostAliasEntries(i), false));
	    	 }
	     }
		
	}
	private Component getHostAliasEntries(final int pos) {
		// TODO Auto-generated method stub
		final FormLayout form = new FormLayout();
		//application Name
		form.setMargin(false);
        form.setSpacing(true);
        form.setWidth("100%");
        //form.set
        form.addStyleName("light");
        String hostname=wasConfigProperty.getString("haHost"+pos+"Name", "");
        String port=wasConfigProperty.getString("haHost"+pos+"Port", "");
        
        final TextField hostnameText = new TextField("Hostname");
        hostnameText.setValue(hostname);
        hostnameText.setWidth("100%");
        hostnameText.addValueChangeListener(new Property.ValueChangeListener(){

   			@Override
   			public void valueChange(ValueChangeEvent event) {
   				// TODO Auto-generated method stub
   				String value = (String) event.getProperty().getValue();
   				((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("haHost"+pos+"Name", value);
   			}
           	
           });
        hostnameText.setImmediate(true);
        form.addComponent(hostnameText);
        
        final TextField portText = new TextField("Port");
        portText.setValue(port);
        portText.setWidth("100%");
        portText.addValueChangeListener(new Property.ValueChangeListener(){

   			@Override
   			public void valueChange(ValueChangeEvent event) {
   				// TODO Auto-generated method stub
   				String value = (String) event.getProperty().getValue();
   				((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("haHost"+pos+"Port", value);
   			}
           	
           });
        portText.setImmediate(true);
        form.addComponent(portText);
		return form;
	}
	

}

